﻿ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using Scopa.SaveData;
using Scopa.GameSound;

namespace Scopa.Menu
{
    using Locale = ScopaLocale;

  public interface ISettingsMenu
  {
    event MenuEvent OnSBackClicked;
    event MenuEvent OnGameSettings;
    event MenuEvent OnGraphicsSettings;
    event MenuEvent OnAudioSettings;
    event MenuEvent OnWIN;
  }

  public class SettingsMenu : BaseMenu, ISettingsMenu
  {

    Button BackToMM; //, gameSettings, audioSettings, graphicsSettings;
    Vector2 savedPos;
    SpriteFont smallFont, largeFont;
    public bool Animated = false;
    public int Direction = 1, currentMenu = 0, currentWord, AnimVelocity = 60;
    Color color;
    SpriteBatch spriteBatch;

    public SettingsMenu(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = null; //game.Content.Load<Texture2D>(@"Images//Menu//SettingsCard");
      bounds = new Rectangle(80, 20, 220, 331);
      game.Services.AddService(typeof(ISettingsMenu), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(ISettingsMenu));
      base.Dispose(disposing);
    }


    public override void Initialize()
    {
      spriteBatch = Game.GetScopaContext().SpriteBatch;

      color = Color.Black;

      savedPos.X = bounds.X;

      // Fonts
      smallFont = Game.Content.Load<SpriteFont>("small_font");
      largeFont = Game.Content.Load<SpriteFont>("large_font");

      BackToMM = new Button(Game, @"Images//Menu//button2", new Rectangle(600, 400, 120, 55), Color.Transparent, "small_font", Locale.Strings.Save, Color.Transparent);
      AddButton(BackToMM, () =>
      {
        BackToMM.touched = true;
        var SoundManager = Game.Services.GetService(typeof(ISoundManager<MenuSounds>)) as ISoundManager<MenuSounds>;
        SoundManager.PlaySound(MenuSounds.ClickCard);
      });
      currentWord = (int)largeFont.MeasureString(Locale.Strings.GameSettings).X;

      base.Initialize();
    }

    int counter = 4;

    public override void Update(GameTime gameTime)
    {
      if (BackToMM.touched)
      {
        counter--;
        if (counter < 0)
        {
          ScopaStateManagement.ScopaStateManager.GameSettings.backClicked = true;
          Animated = true;
        
        }
      }

      if (menuDepth == MenuDepths.FrontMenu)
      {
        BackToMM.color = Color.MediumBlue;
        BackToMM.textColor = Color.White;
      }

      // Input: back button
      if (((Game1)Game).IsBackButtonPressed())
      {
        // Button events
        //StateManagement.StateManager.audioSettings.backClicked = true;
        ScopaStateManagement.ScopaStateManager.GameSettings.backClicked = true;
        //StateManagement.StateManager.gameSettings.backClicked = true;
        Animated = true;
      }
      
      if (Animated)
      {
        bounds.X += Direction * AnimVelocity;

        foreach (Button b in (from cb in buttons select cb.b))
        {
            b.bounds.X += Direction * AnimVelocity;
        }
        
        if (bounds.X > (savedPos.X + bounds.Width + 30))
        {          
          Direction = -1;
          if (menuDepth == MenuDepths.MiddleMenu) //Entry Animation
          {
            menuDepth = MenuDepths.FrontMenu;
            DrawOrder = (int)menuDepth;
          }
          else //Exit Animation
          {
            menuDepth = MenuDepths.MiddleMenu;
            DrawOrder = (int)menuDepth;
          }
        }

        if (bounds.X < savedPos.X)
        {
          int offset = (int)savedPos.X - bounds.X;
          Animated = false;
          Direction = 1;
          bounds.X = (int)savedPos.X;
          //StateManagement.StateManager.gameSettings.Enabled = true;

          foreach (Button b in (from cb in buttons select cb.b))
          {
              b.bounds.X += Direction * offset;
          }

          if (menuDepth == MenuDepths.MiddleMenu)
          {
            if (OnSBackClicked != null)
              OnSBackClicked();
          }
        }
      }

      base.Update(gameTime);
    }


    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);
    }

    public event MenuEvent OnSBackClicked;
    public event MenuEvent OnGameSettings;
    public event MenuEvent OnGraphicsSettings;
    public event MenuEvent OnAudioSettings;
    public event MenuEvent OnWIN;
  }
}
